package com.detrum.costomview;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Switch;
import android.widget.TextView;

import com.detrum.base.R;
import com.detrum.bean.StateBean;

/**
 * Created by abc on 2017/8/18.
 */

public class StateShow extends RelativeLayout {
    private Context context;
    private ImageView image;
    private TextView text;
    private Switch switch1;
    private int img;
    private float rotation;
    private String info;
    private boolean isShow;
    private StateShowCall stateShowCall;
    public StateShow(Context context) {
        super(context);
        this.context=context;
    }

    public StateShow(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context=context;
        LayoutInflater.from(context).inflate(R.layout.state_show,this,true);
        TypedArray typedArray=context.obtainStyledAttributes(attrs, R.styleable.StateShow);
        img=typedArray.getResourceId(R.styleable.StateShow_image,0);
        info=typedArray.getString(R.styleable.StateShow_text);
        isShow=typedArray.getBoolean(R.styleable.StateShow_select,false);
        rotation=typedArray.getFloat(R.styleable.StateShow_rotation,0);
        typedArray.recycle();
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        image= (ImageView) findViewById(R.id.image);
        text= (TextView) findViewById(R.id.text);
        switch1= (Switch) findViewById(R.id.switch1);
        image.setRotation(rotation);
        if(img!=0){
            image.setImageResource(img);
        }
        if(!TextUtils.isEmpty(info)){
            text.setText(info);
        }
        switch1.setChecked(isShow);
        switch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                stateShowCall.Select(StateShow.this,isChecked);
            }
        });
    }

    public void setStateShowCall(StateShowCall stateShowCall){
        this.stateShowCall=stateShowCall;
    }

    public void setImg(int img){
        image.setImageResource(img);
    }

    public void setText(String str){
        text.setText(str);
    }

    public void setSwitch(boolean is){
        switch1.setChecked(is);
    }

    public String getText(){
        return text.getText().toString();
    }

    public boolean getSwitch(){
        return switch1.isChecked();
    }

    public void setData(StateBean bean){
        if(bean!=null){
            if(bean.getImage()!=0){
                image.setImageResource(bean.getImage());
            }
            text.setText(bean.getText());
            switch1.setChecked(bean.isshow());
        }
    }
}
